<template>
  <a-row class="grid-demo" align="center" :wrap="false">
    <a-col flex="auto">
      <div class="globalHeader">
        <a-menu
          mode="horizontal"
          :selected-keys="selectedKeys"
          @menu-item-click="doMenuClick"
        >
          <a-menu-item
            key="0"
            :style="{ padding: 0, marginRight: '38px' }"
            disabled
          >
            <router-link to="/">
              <div class="titleBar">
                <img class="logo" src="../assets/logo.png" alt="logo" />
                <div class="title">秋呀 OJ</div>
              </div>
            </router-link>
          </a-menu-item>
          <a-menu-item v-for="item in visibleRoutes" :key="item.path">
            {{ item.name }}
          </a-menu-item>
        </a-menu>
      </div>
    </a-col>
    <a-col flex="100px">
      <div>{{ store.state.user?.loginUser?.userName ?? "未登录" }}</div>
    </a-col>
  </a-row>
</template>

<script lang="ts" setup>
import { routes } from "@/router/routes";
import router from "@/router";
import { computed, ref } from "vue";
import { useStore } from "vuex";
import checkAccess from "@/access/checkAccess";
import accessEnum from "@/access/accessEnum";

const doMenuClick = (key: string) => {
  router.push({ path: key });
};

const selectedKeys = ref<string>();
router.afterEach((to) => {
  selectedKeys.value = [to.path];
});

const store = useStore();

const visibleRoutes = computed(() => {
  return routes.filter((item, index) => {
    if (item.meta?.hideInMenu) {
      return false;
    }
    // 根据权限过滤菜单
    if (
      !checkAccess(store.state.user.loginUser, item?.meta?.access as string)
    ) {
      return false;
    }
    return true;
  });
});

// setTimeout(() => {
//   store.dispatch("user/getLoginUser", {
//     userName: "秋呀",
//     userRole: accessEnum.ADMIN,
//   });
// }, 3000);
</script>

<style scoped>
.titleBar {
  display: flex;
  align-items: center;
}

.title {
  color: black;
  font-size: 18px;
  margin-left: 16px;
}

.logo {
  height: 48px;
  width: 48px;
}
</style>
